package com.utils;

import java.io.InputStream;
import java.io.InputStreamReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class JSUtils {

	public static ScriptEngine scriptEngine = null;
	
	public static void main(String[] args) throws Exception{
		System.out.println(getMediaRegisterPwd("13800138000", "11111111", "i37ibi4k51"));
//		System.out.println(getMediaRegisterPwd(getMediaRegisterPwd("11111111")));
//		System.out.println(getMediaRegisterPwd(getMediaRegisterPwd(getMediaRegisterPwd("11111111"))+"i37ibi4k51"+"13800138000"));
	}
	
	/**
	 * 美的js密码加密
	 * @param password 密码
	 * @return
	 */
	public static String getMediaRegisterPwd(String mobile, String passwd, String nonceid) {
		
		try {
			if(scriptEngine == null){
				scriptEngine = new ScriptEngineManager().getEngineByName("js");
				InputStream inputStream = JSUtils.class.getClassLoader().getResourceAsStream("midea.js");
				scriptEngine.eval(new InputStreamReader(inputStream));
			}
			Invocable invocable=(Invocable)scriptEngine;
			Object object = invocable.invokeFunction("hex_md5", passwd);
			object = invocable.invokeFunction("hex_md5", object.toString());
			object = invocable.invokeFunction("hex_md5", object.toString()+nonceid+mobile);
			return object.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
